<template>
	<view>
		<nav-bar :titleCenter="false">
			<view>
				<view class="search_box"><input type="text" placeholder="搜索" v-model="keyWord" confirm-type="search" @confirm="onSearch"/></view>
			</view>
			<view class="icon_setUp" slot="right" @click="onSearch">搜索</view>
		</nav-bar>
        <!-- 公共组件-每个页面必须引入 -->
        <public-module></public-module>
        <mescroll-body ref="mescrollRef" @init="mescrollInit" :down="downOption" @down="downCallback" @up="upCallback">
            <waterfall-goods ref="waterfall" :list="goodsDataList"></waterfall-goods>
        </mescroll-body>
	</view>
</template>

<script>
	import waterfallGoods from '@/components/module/waterfall_goods';
import MescrollMixin from "@/components/common/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
	components: {
		waterfallGoods
	},
	data() {
		return {
			downOption: {
				auto: false //是否在初始化后,自动执行downCallback; 默认true
			},
			goodsDataList: [],
			objId:"",
			keyWord:""
		};
	},
	//第一次加载
	onLoad(e) {
		if(e.objId){
			this.objId = e.objId;
		}
	},
	//页面显示
	onShow() {},
	//方法
	methods: {
		onPageJump(url) {
			uni.navigateTo({
				url: url
			});
		},
		// 点击搜索
		onSearch() {
			if (this.keyWord) {
				this.mescroll.triggerDownScroll();
			}else{
				uni.showToast({
					title:"请输入关键字",
					icon:"none"
				})
			}
		},
        /*下拉刷新的回调 */
        downCallback() {
        	//联网加载数据
        	this.loadData(1);
        },
        /*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
        upCallback(page) {
        	//联网加载数据
        	this.loadData(page.num);
        },
        loadData(pageNo) {
            let httpData = {
                pageNo: pageNo,
                pageSize: 15,
                shopId: this.objId
            };
            if(this.keyWord){
            	httpData.keyWord = this.keyWord;
            }
            this.$http
            	.post('api/mall/goods/v1/list', httpData,{
            		load:false
            	}).then(res => {
            		uni.stopPullDownRefresh();
            		//方法一(推荐): 后台接口有返回列表的总页数 totalPage
            		this.mescroll.endByPage(res.data.length, res.pages); //必传参数(当前页的数据个数, 总页数)
            		
            		if (pageNo == 1) {
            			this.goodsDataList = res.data;
            		} else {
            			this.goodsDataList = this.goodsDataList.concat(res.data);
            		}
            	}).catch(() => {
            		//联网失败, 结束加载
            		this.mescroll.endErr();
            	});
        },
	},
	//页面隐藏
	onHide() {},
	//页面卸载
	onUnload() {},
	//用户点击分享
	onShareAppMessage(e) {
		return this.wxShare();
	}
};
</script>
<style lang="scss" scoped>
@import '@/style/mixin.scss';
.icon_setUp {
	font-size: 30rpx;
	margin-right: 30rpx;
}
.search_box {
	/* #ifdef MP */
	width: 360rpx;
	/* #endif */
	/* #ifndef MP */
	width: 520rpx;
	/* #endif */
	height: 64rpx;
	background: #f7f7f7;
	border-radius: 37rpx;
	display: flex;
	align-items: center;
	padding: 0 20rpx 0 40rpx;
	&:before {
		content: '';
		background-image: url('../../../static/icon/ic_search.png');
		background-size: 100% 100%;
		background-repeat: no-repeat;
		background-position: center center;
		width: 30rpx;
		height: 30rpx;
		margin-right: 20rpx;
	}

	input {
		font-size: 28rpx;
		height: 64rpx;
		font-weight: normal;
	}
}
</style>
